package com.yw.common.core.db.dao.impl;

import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yw.common.core.db.IDBConnection;
import com.yw.common.core.db.dao.entity.IPo;
import com.yw.common.core.db.exception.DBCloseException;
import com.yw.common.core.db.test.entity.PoStudent;
import com.yw.common.core.util.DbUtil;
import com.yw.common.core.util.StringUtil;

/**
 * 数据库更新dao默认实现
 * @author aoshiguchen
 * @time 2015-12-03
 */

public class DefaultUpdateDaoImpl extends AbstractUpdateDaoImpl{
	private Class<IPo> entityClass;
	
	public DefaultUpdateDaoImpl(String tbName) {
		super(tbName);
	}
	
	public DefaultUpdateDaoImpl(Class entityClass,String tbName) {
		super(tbName);
		
		this.entityClass = entityClass;
	}
	
	public DefaultUpdateDaoImpl(Class entityClass) {
		String[] s = entityClass.getName().split("\\.");

		this.tbName = s[s.length - 1].substring(2);

		this.entityClass = entityClass;
	}
	
	@Override
	public List<IPo> findAll() {
		List<IPo> pos = new ArrayList<IPo>();
		Map<String,Class> fs = getFieldSet();
		
		IDBConnection conn = DbUtil.getDBConnection();
		
		try{
			
			conn.open(); 
			
			PreparedStatement ps = conn.prepareStatement("select * from " + tbName);
			ResultSet rs = ps.executeQuery();
			
			while(rs.next()){
				//IPo po = new PoStudent();
				IPo po = entityClass.newInstance();
				
				for(String  field : fs.keySet()){
					Object value = rs.getObject(field);
					
					Method m = entityClass.getDeclaredMethod(StringUtil.getSetMethodName(field),fs.get(field));
					m.invoke(po,value);
				}
				pos.add(po);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (DBCloseException e) {
				e.printStackTrace();
			}
		}
		
		return pos;
	}
	
	@Override
	public Map<String, Class> getFieldSet() {
		if(null == fieldSet){
			fieldSet = new HashMap<String, Class>();

			IDBConnection conn = DbUtil.getDBConnection();
			
			try{
				conn.open();
				
				PreparedStatement ps = conn.prepareStatement("SELECT Name FROM SysColumns WHERE id=Object_Id('" + tbName + "')");
				
				ResultSet rs = ps.executeQuery();
				
				while(rs.next()){
					//System.out.println(entityClass.getDeclaredField(rs.getString("name")).getType());
					//System.out.println(rs.getString("name"));
					
					fieldSet.put(rs.getString("name"), entityClass.getDeclaredField(rs.getString("name")).getType());
				}
				
				conn.close();
				
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					conn.close();
				} catch (DBCloseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
			
			
			
//			fieldSet.put("id", String.class);
//			fieldSet.put("name", String.class);
//			fieldSet.put("sex", Integer.class);
//			fieldSet.put("age", Integer.class);
//			fieldSet.put("classid", String.class);
		}
		
		return fieldSet;
	}

}
